<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Article;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Category;
use Illuminate\Support\Facades\Auth;
class ReportController extends Controller
{
    protected $name = '报告下载';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Article::class, function (Grid $grid) {
            $grid->model()->where('post_type', 32)->orderBy('id', 'DESC');
            
            $grid->filter(function($filter){
            
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                
                //$filter->useModal();
            
                // 在这里添加字段过滤器
                
                $filter->like('post_title', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                
            
            });
    
            $grid->column('id', 'ID')->sortable();
            $grid->column('post_title', '名称');
            
            $grid->column('price', '价格');
            $grid->column('good', '评分');
            $grid->column('author', '作者');
            
            $grid->column('updated_at', '更新时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(Article::class, function (Form $form) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('post_title', '标题')->rules('required|min:3');
            
            $form->file('download', '文档地址');
            $form->number('price', '价格');
            $form->number('good', '质量')->help('1-5分');
            $form->text('author', '贡献者');
            $form->text('post_source', '文档格式')->help('word，Excel，pdf，ppt');
            
            $form->text('post_hits', '下载次数')->default(rand(50,100));
            
            $form->hidden('post_type')->default('32');
            
            
        });
    }
}
